<?PHP
  $template = new Template();
  $template->load("sites");
  $template->assign_var("FORMURL","/admin/index.php?page=sites&dir=".urlencode($_GET['dir']));
 
  $dir = $_GET['dir'];
 
  if($_POST['newFolder']){
    FileServer::createFolder("../content/articles/".$dir,$_POST['name']);
  }
  if($_GET['rmdir']){
    rmdir("../content/articles/".$_GET['rmdir']);
    $template->assign_var("MESSAGE","Verzeichnis ".$_GET['rmdir']." wurde gel&ouml;scht!");
  }
  else{
    $template->assign_var("MESSAGE","");
  }
  
  $verzeichnis = openDir("../content/articles/".$dir);
 
  if($dir){
    $template->assign_var("NEWPAGELINK","<a href=\"/admin/index.php?page=site-new&dir=".substr($dir,1)."\">Neue Seite</a>");
  }
  else{
    $template->assign_var("NEWPAGELINK"," <a href=\"/admin/index.php?page=site-new\">Neue Seite</a>");
  }
  $pre = "";
  foreach(split("/",$dir) as $cDir){
    $index = $template->add_loop_item("PATH");
    $path .= "/".$cDir;
    if($path == "/"){
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=sites");
      $template->assign_loop_var("PATH", $index, "LABEL","/");
      $path = "";
    }
    else{
      $template->assign_loop_var("PATH", $index, "URL","/admin/index.php?page=sites&dir=".$path);
      $template->assign_loop_var("PATH", $index, "LABEL",$cDir);
    }
  }
  $subFolders = FileServer::getFolders("../content/articles/".$dir);
  if($subFolders){
    foreach($subFolders as $folder){
      $index = $template->add_loop_item("DIRS");
	  $template->assign_loop_var("DIRS",$index,"URL","/admin/index.php?page=sites&dir=".$dir."/".$folder);
	  $template->assign_loop_var("DIRS",$index,"LABEL",$folder);
	}
  }
  if(trim($_GET['dir']) != "" & trim($_GET['dir']) != "/"){
    $template->assign_var("DELETEFOLDERLINK","<a href=\"/admin/index.php?page=sites&rmdir=".$_GET['dir']."\">Ordner l&ouml;schen</a>");
  }
  else{
    $template->assign_var("DELETEFOLDERLINK","");
  }
  $pages = Page::getPagesByDir($dir);
  if(substr($dir,0,1) == "/"){
    $shortdir = substr($dir,1);
  }
  else{
    $shortdir = $dir;
  }
  if($pages){
    foreach($pages as $page){
	  $shortalias = substr($page->alias,strlen($shortdir));
	  if(substr($shortalias,0,1) == "/"){
	    $shortalias = substr($shortalias,1);
	  }
	  else{
	    $shortalias = $shortalias;
	  }
	  if(!strpos($shortalias,"/")){
        $index = $template->add_loop_item("PAGES");
	    $template->assign_loop_var("PAGES",$index,"TITLE",$page->title);
	    $template->assign_loop_var("PAGES",$index,"ALIAS",$shortalias);
	    $template->assign_loop_var("PAGES",$index,"EDITURL","index.php?page=site-edit&site=".$page->alias);
	    $template->assign_loop_var("PAGES",$index,"DELETEURL","index.php?page=site-delete&site=".$page->alias);
	  }
    }
   $template->assign_var("NOPAGES","");
  }
  else{
   $template->assign_var("NOPAGES","Es sind keine Seiten vorhanden.");
  }
  $template->output();
?>